package com.archgeek.scala.code

import com.archgeek.scala.code.BuilderStatusEnums.{BuilderStatusEnums, SUCCESS}

import java.util.concurrent.TimeUnit
import scala.annotation.tailrec

/**
 *
 * @author pizhihui
 * @date 2023-12-02 14:35
 */
object LoadServerDemo {


  // https://softwaremill.com/retrying-function-calls-in-scala/
  @tailrec
  def retry[T](maxRetries: Int, n: Int = 0)(fn: Int => T): T = {
    try {
      fn(n)
    } catch {
      case e: Exception =>
        if (n < maxRetries-1 ) retry(maxRetries,n + 1)(fn)
        else throw e
    }
  }

  def shellExec(n: Int): Unit = {
    println(s"${} 开始任务执行 xxx ${n}")
    TimeUnit.SECONDS.sleep(5)

    try {
      val n1 = 3/1
    }catch {
      case e: Exception =>
       println("执行错误...", e)
        throw e
    }
  }

  def main(args: Array[String]): Unit = {

    var priority: Option[String] = None

    priority = Some("SPECIAL")

    priority match {
      case Some(x) =>
        if(x.nonEmpty && x.equals("SPECIAL")) {

        }
      case None => println(s"error ")
    }


//    retry(3){retryCount =>
//      shellExec(retryCount)
//    }


//
//    val list = ListBuffer[String]()
//
//    list += "java"
//    list += "hello"
//
//    println(list.toList.mkString)
//
//    val st = null
//    Option(st).foreach { x =>
//      println(s"数据是: ${x}")
//    }
//
//
    var enums: BuilderStatusEnums = null
//    enums = SUCCESS
    Option(enums).foreach{ x =>
      println(s"枚举值是: ${x}")
    }

//    val num1: Int
//    Option(num1).foreach{ x =>
//      println(s"数字是: ${x}")
//    }

//
//    val maxRetries = 3
//    (0 until maxRetries).foreach { retryCount =>
//      println(retryCount)
//
//      try {
//        TimeUnit.SECONDS.sleep(3)
//        val res = 3/0
//        println(res)
//      } catch {
//        case e: Exception =>
//          println("error")
//          if(retryCount >= maxRetries-1){
//            throw e
//          }
//      }
//
//    }

  }

}
